home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_108 / bash-108.zoo / bash-1.08 / readline / doc / readline.info < prev    next >
Encoding:
Text File  |  1991-05-01  |  55.7 KB  |  1,644 lines

  1. Info file readline.info, produced by Makeinfo, -*- Text -*- from
  2. input file /usr/gnu/src/lib/readline/doc/rlman.texinfo.
  3.  
  4. This document describes the GNU Readline Library, a utility for
  5. aiding in the consitency of user interface across discrete
  6. programs that need to provide a command line interface.
  7.  
  8. Copyright (C) 1988, 1991 Free Software Foundation, Inc.
  9.  
  10. Permission is granted to make and distribute verbatim copies of
  11. this manual provided the copyright notice and this permission
  12. notice pare preserved on all copies.
  13.  
  14. Permission is granted to copy and distribute modified versions of
  15. this manual under the conditions for verbatim copying, provided
  16. that the entire resulting derived work is distributed under the
  17. terms of a permission notice identical to this one.
  18.  
  19. Permission is granted to copy and distribute translations of this
  20. manual into another language, under the above conditions for
  21. modified versions, except that this permission notice may be
  22. stated in a translation approved by the Foundation.
  23.  
  24. File: readline.info,  Node: Top,  Next: Command Line Editing,  Prev: (DIR),  Up: (DIR)
  25.  
  26. GNU Readline Library
  27. ********************
  28.  
  29. This document describes the GNU Readline Library, a utility for
  30. aiding in the consistency of user interface across discrete
  31. programs that need to provide a command line interface.
  32.  
  33. * Menu:
  34.  
  35. * Command Line Editing::       GNU Readline User's Manual.
  36. * Programming with GNU Readline::  GNU Readline Programmer's Manual.
  37. * Concept Index::           Index of concepts described in this manual.
  38. * Function and Variable Index::       Index of externally visible functions
  39.                    and variables.
  40.  
  41. File: readline.info,  Node: Command Line Editing,  Next: Programming with GNU Readline,  Prev: Top,  Up: Top
  42.  
  43. Command Line Editing
  44. ********************
  45.  
  46. This text describes GNU's command line editing interface.
  47.  
  48. * Menu:
  49.  
  50. * Introduction and Notation::    Notation used in this text.
  51. * Readline Interaction::    The minimum set of commands for editing a line.
  52. * Readline Init File::        Customizing Readline from a user's view.
  53.  
  54. File: readline.info,  Node: Introduction and Notation,  Next: Readline Interaction,  Up: Command Line Editing
  55.  
  56. Introduction to Line Editing
  57. ============================
  58.  
  59. In this tex a the following notation is used to describe keystrokes.
  60.  
  61. The text C-k is read as `Control-K' and describes the character
  62. produced
  63. when the Control key is depressed and the k key is struck.
  64.  
  65. The text M-k is read as `Meta-K' and describes the character
  66. produced when the meta key (if you have one) is depressed, and the
  67. k key is struck.  If you do not have a meta key, the identical
  68. keystroke can be generated by typing ESC first, and then typing k.
  69. Either process is known as "metafying" the k key.
  70.  
  71. The text M-C-k is read as `Meta-Control-k' and describes the
  72. character produced by "metafying" C-k.
  73.  
  74. In addition, several keys have their own names.  Specifically, DEL,
  75. ESC, LFD, SPC, RET, and TAB all stand for themselves when seen in
  76. this text, or in an init file (*note Readline Init File::., for
  77. more info).
  78.  
  79. File: readline.info,  Node: Readline Interaction,  Next: Readline Init File,  Prev: Introduction and Notation,  Up: Command Line Editing
  80.  
  81. Readline Interaction
  82. ====================
  83.  
  84. Often during an interactive session you type in a long line of
  85. text, only to notice that the first word on the line is
  86. misspelled.  The Readline library gives you a set of commands for
  87. manipulating the text as you type it in, allowing you to just fix
  88. your typo, and not forcing you to retype the majority of the line.
  89. Using these editing commands, you move the cursor to the place
  90. that needs correction, and delete or insert the text of the
  91. corrections.  Then, when you are satisfied with the line, you
  92. simply press RETURN.  You do not have to be at the end of the
  93. line to press RETURN; the entire line is accepted regardless of
  94. the location of the cursor within the line.
  95.  
  96. * Menu:
  97.  
  98. * Readline Bare Essentials::    The least you need to know about Readline.
  99. * Readline Movement Commands::    Moving about the input line.
  100. * Readline Killing Commands::    How to delete text, and how to get it back!
  101. * Readline Arguments::        Giving numeric arguments to commands.
  102.  
  103. File: readline.info,  Node: Readline Bare Essentials,  Next: Readline Movement Commands,  Up: Readline Interaction
  104.  
  105. Readline Bare Essentials
  106. ------------------------
  107.  
  108. In order to enter characters into the line, simply type them.  The
  109. typed character appears where the cursor was, and then the cursor
  110. moves one space to the right.  If you mistype a character, you can
  111. use DEL to back up, and delete the mistyped character.
  112.  
  113. Sometimes you may miss typing a character that you wanted to type,
  114. and not notice your error until you have typed several other
  115. characters.  In that case, you can type C-b to move the cursor to
  116. the left, and then correct your mistake.  Aftwerwards, you can
  117. move the cursor to the right with C-f.
  118.  
  119. When you add text in the middle of a line, you will notice that
  120. characters to the right of the cursor get `pushed over' to make
  121. room for the text that you have inserted.  Likewise, when you
  122. delete text behind the cursor, characters to the right of the
  123. cursor get `pulled back' to fill in the blank space created by the
  124. removal of the text.  A list of the basic bare essentials for
  125. editing the text of an input line follows.
  126.  
  127. C-b
  128.      Move back one character.
  129.  
  130. C-f
  131.      Move forward one character.
  132.  
  133. DEL
  134.      Delete the character to the left of the cursor.
  135.  
  136. C-d
  137.      Delete the character underneath the cursor.
  138.  
  139. Printing characters
  140.      Insert itself into the line at the cursor.
  141.  
  142. C-_
  143.      Undo the last thing that you did.  You can undo all the way
  144.      back to an empty line.
  145.  
  146. File: readline.info,  Node: Readline Movement Commands,  Next: Readline Killing Commands,  Prev: Readline Bare Essentials,  Up: Readline Interaction
  147.  
  148. Readline Movement Commands
  149. --------------------------
  150.  
  151. The above table describes the most basic possible keystrokes that
  152. you need in order to do editing of the input line.  For your
  153. convenience, many other commands have been added in addition to
  154. C-b, C-f, C-d, and DEL.  Here are some commands for moving more
  155. rapidly about the line.
  156.  
  157. C-a
  158.      Move to the start of the line.
  159.  
  160. C-e
  161.      Move to the end of the line.
  162.  
  163. M-f
  164.      Move forward a word.
  165.  
  166. M-b
  167.      Move backward a word.
  168.  
  169. C-l
  170.      Clear the screen, reprinting the current line at the top.
  171.  
  172. Notice how C-f moves forward a character, while M-f moves forward a
  173. word.  It is a loose convention that control keystrokes operate on
  174. characters while meta keystrokes operate on words.
  175.  
  176. File: readline.info,  Node: Readline Killing Commands,  Next: Readline Arguments,  Prev: Readline Movement Commands,  Up: Readline Interaction
  177.  
  178. Readline Killing Commands
  179. -------------------------
  180.  
  181. The act of "cutting" text means to delete the text from the line,
  182. and to save away the deleted text for later use, just as if you
  183. had cut the text out of the line with a pair of scissors.  There
  184. is a
  185.  
  186. "Killing" text means to delete the text from the line, but to save
  187. it away for later use, usually by "yanking" it back into the line.
  188. If the description for a command says that it `kills' text, then
  189. you can be sure that you can get the text back in a different (or
  190. the same) place later.
  191.  
  192. Here is the list of commands for killing text.
  193.  
  194. C-k
  195.      Kill the text from the current cursor position to the end of
  196.      the line.
  197.  
  198. M-d
  199.      Kill from the cursor to the end of the current word, or if
  200.      between words, to the end of the next word.
  201.  
  202. M-DEL
  203.      Kill fromthe cursor the start ofthe previous word, or if
  204.      between words, to the start of the previous word.
  205.  
  206. C-w
  207.      Kill from the cursor to the previous whitespace.  This is
  208.      different than M-DEL because the word boundaries differ.
  209.  
  210. And, here is how to "yank" the text back into the line.  Yanking is
  211.  
  212. C-y
  213.      Yank the most recently killed text back into the buffer at the
  214.      cursor.
  215.  
  216. M-y
  217.      Rotate the kill-ring, and yank the new top.  You can only do
  218.      this if the prior command is C-y or M-y.
  219.  
  220. When you use a kill command, the text is saved in a "kill-ring". 
  221. Any number of consecutive kills save all of the killed text
  222. together, so that when you yank it back, you get it in one clean
  223. sweep.  The kill ring is not line specific; the text that you
  224. killed on a previously typed line is available to be yanked back
  225. later, when you are typing another line.
  226.  
  227. File: readline.info,  Node: Readline Arguments,  Prev: Readline Killing Commands,  Up: Readline Interaction
  228.  
  229. Readline Arguments
  230. ------------------
  231.  
  232. You can pass numeric arguments to Readline commands.  Sometimes the
  233. argument acts as a repeat count, other times it is the sign of the
  234. argument that is significant.  If you pass a negative argument to
  235. a command which normally acts in a forward direction, that command
  236. will act in a backward direction.  For example, to kill text back
  237. to the start of the line, you might type M-- C-k.
  238.  
  239. The general way to pass numeric arguments to a command is to type
  240. meta digits before the command.  If the first `digit' you type is
  241. a
  242. minus sign (-), then the sign of the argument will be negative. 
  243. Once you have typed one meta digit to get the argument started,
  244. you
  245. can type the remainder of the digits, and then the command.  For
  246. example, to give the C-d command an argument of 10, you could type
  247. M-1 0 C-d.
  248.  
  249. File: readline.info,  Node: Readline Init File,  Prev: Readline Interaction,  Up: Command Line Editing
  250.  
  251. Readline Init File
  252. ==================
  253.  
  254. Although the Readline library comes with a set of Emacs-like
  255. keybindings, it is possible that you would like to use a different
  256. set of keybindings.  You can customize programs that use Readline
  257. by putting commands in an "init" file in your home directory.  The
  258. name
  259. of this file is `~/.inputrc'.
  260.  
  261. When a program which uses the Readline library starts up, the
  262. `~/.inputrc' file is read, and the keybindings are set.
  263.  
  264. * Menu:
  265.  
  266. * Readline Init Syntax::    Syntax for the commands in `~/.inputrc'.
  267. * Readline Vi Mode::        Switching to `vi' mode in Readline.
  268.  
  269. File: readline.info,  Node: Readline Init Syntax,  Next: Readline Vi Mode,  Up: Readline Init File
  270.  
  271. Readline Init Syntax
  272. --------------------
  273.  
  274. You can start up with a vi-like editing mode by placing
  275.  
  276.      `set editing-mode vi'
  277.  
  278. in your `~/.inputrc' file.
  279.  
  280. You can have Readline use a single line for display, scrolling the
  281. input between the two edges of the screen by placing
  282.  
  283.      `set horizontal-scroll-mode On'
  284.  
  285. in your `~/.inputrc' file.
  286.  
  287. The syntax for controlling keybindings in the `~/.inputrc' file is
  288. simple.  First you have to know the name of the command that you
  289. want to change.  The following pages contain tables of the command
  290. name, the default keybinding, and a short description of what the
  291. command does.
  292.  
  293. Once you know the name of the command, simply place the name of the
  294. key you wish to bind the command to, a colon, and then the name of
  295. the command on a line in the `~/.inputrc' file.  Here is an example:
  296.  
  297.      # This is a comment line.
  298.      Meta-Rubout:    backward-kill-word
  299.      Control-u:    universal-argument
  300.  
  301. * Menu:
  302.  
  303. * Commands For Moving::        Moving about the line.
  304. * Commands For History::    Getting at previous lines.
  305. * Commands For Text::        Commands for changing text.
  306. * Commands For Killing::    Commands for killing and yanking.
  307. * Numeric Arguments::        Specifying numeric arguments, repeat counts.
  308. * Commands For Completion::    Getting Readline to do the typing for you.
  309. * Miscellaneous Commands::    Other miscillaneous commands.
  310.  
  311. File: readline.info,  Node: Commands For Moving,  Next: Commands For History,  Up: Readline Init Syntax
  312.  
  313. Commands For Moving
  314. ...................
  315.  
  316. `beginning-of-line (C-a)'
  317.      Move to the start of the current line.
  318.  
  319. `end-of-line (C-e)'
  320.      Move to the end of the line.
  321.  
  322. `forward-char (C-f)'
  323.      Move forward a character.
  324.  
  325. `backward-char (C-b)'
  326.      Move back a character.
  327.  
  328. `forward-word (M-f)'
  329.      Move forward to the end of the next word.
  330.  
  331. `backward-word (M-b)'
  332.      Move back to the start of this, or the previous, word.
  333.  
  334. `clear-screen (C-l)'
  335.      Clear the screen leaving the current line at the top of the
  336.      screen.
  337.  
  338. File: readline.info,  Node: Commands For History,  Next: Commands For Text,  Prev: Commands For Moving,  Up: Readline Init Syntax
  339.  
  340. Commands For Manipulating The History
  341. .....................................
  342.  
  343. `accept-line (Newline, Return)'
  344.      Accept the line regardless of where the cursor is.  If this
  345.      line is non-empty, add it too the history list.  If this line
  346.      was a history line, then restore the history line to its
  347.      original state.
  348.  
  349. `previous-history (C-p)'
  350.      Move `up' through the history list.
  351.  
  352. `next-history (C-n)'
  353.      Move `down' through the history list.
  354.  
  355. `beginning-of-history (M-<)'
  356.      Move to the first line in the history.
  357.  
  358. `end-of-history (M->)'
  359.      Move to the end of the input history, i.e., the line you are
  360.      entering!
  361.  
  362. `reverse-search-history (C-r)'
  363.      Search backward starting at the current line and moving `up'
  364.      through the history as necessary.  This is an incremental
  365.      search.
  366.  
  367. `forward-search-history (C-s)'
  368.      Search forward starting at the current line and moving `down'
  369.      through the the history as neccessary.
  370.  
  371. File: readline.info,  Node: Commands For Text,  Next: Commands For Killing,  Prev: Commands For History,  Up: Readline Init Syntax
  372.  
  373. Commands For Changing Text
  374. ..........................
  375.  
  376. `delete-char (C-d)'
  377.      Delete the character under the cursor.  If the cursor is at
  378.      the beginning of the line, and there are no characters in the
  379.      line, and the last character typed was not C-d, then return
  380.      EOF.
  381.  
  382. `backward-delete-char (Rubout)'
  383.      Delete the character behind the cursor.  A numeric arg says to
  384.      kill the characters instead of deleting them.
  385.  
  386. `quoted-insert (C-q, C-v)'
  387.      Add the next character that you type to the line verbatim. 
  388.      This is how to insert things like C-q for example.
  389.  
  390. `tab-insert (M-TAB)'
  391.      Insert a tab character.
  392.  
  393. `self-insert (a, b, A, 1, !, ...)'
  394.      Insert yourself.
  395.  
  396. `transpose-chars (C-t)'
  397.      Drag the character before point forward over the character at
  398.      point.  Point moves forward as well.  If point is at the end
  399.      of the line, then transpose the two characters before point. 
  400.     
  401.      Negative args don't work.
  402.  
  403. `transpose-words (M-t)'
  404.      Drag the word behind the cursor past the word in front of the
  405.      cursor moving the cursor over that word as well.
  406.  
  407. `upcase-word (M-u)'
  408.      Uppercase the current (or following) word.  With a negative
  409.      argument, do the previous word, but do not move point.
  410.  
  411. `downcase-word (M-l)'
  412.      Lowercase the current (or following) word.  With a negative
  413.      argument, do the previous word, but do not move point.
  414.  
  415. `capitalize-word (M-c)'
  416.      Uppercase the current (or following) word.  With a negative
  417.      argument, do the previous word, but do not move point.
  418.  
  419. File: readline.info,  Node: Commands For Killing,  Next: Numeric Arguments,  Prev: Commands For Text,  Up: Readline Init Syntax
  420.  
  421. Killing And Yanking
  422. ...................
  423.  
  424. `kill-line (C-k)'
  425.      Kill the text from the current cursor position to the end of
  426.      the line.
  427.  
  428. `backward-kill-line ()'
  429.      Kill backward to the beginning of the line.  This is normally
  430.      unbound.
  431.  
  432. `kill-word (M-d)'
  433.      Kill from the cursor to the end of the current word, or if
  434.      between words, to the end of the next word.
  435.  
  436. `backward-kill-word (M-DEL)'
  437.      Kill the word behind the cursor.
  438.  
  439. `unix-line-discard (C-u)'
  440.      Do what C-u used to do in Unix line input.  We save the killed
  441.      text on the kill-ring, though.
  442.  
  443. `unix-word-rubout (C-w)'
  444.      Do what C-w used to do in Unix line input.  The killed text is
  445.      saved on the kill-ring.  This is different than
  446.      backward-kill-word because the word boundaries differ.
  447.  
  448. `yank (C-y)'
  449.      Yank the top of the kill ring into the buffer at point.
  450.  
  451. `yank-pop (M-y)'
  452.      Rotate the kill-ring, and yank the new top.  You can only do
  453.      this if the prior command is yank or yank-pop.
  454.  
  455. File: readline.info,  Node: Numeric Arguments,  Next: Commands For Completion,  Prev: Commands For Killing,  Up: Readline Init Syntax
  456.  
  457. Specifying Numeric Arguments
  458. ............................
  459.  
  460. `digit-argument (M-0, M-1, ... M--)'
  461.      Add this digit to the argument already accumulating, or start
  462.      a new argument.  M-- starts a negative argument.
  463.  
  464. `universal-argument ()'
  465.      Do what C-u does in emacs.  By default, this is not bound.
  466.  
  467. File: readline.info,  Node: Commands For Completion,  Next: Miscellaneous Commands,  Prev: Numeric Arguments,  Up: Readline Init Syntax
  468.  
  469. Letting Readline Type For You
  470. .............................
  471.  
  472. `complete (TAB)'
  473.      Attempt to do completion on the text before point.  This is
  474.      implementation defined.  Generally, if you are typing a
  475.     
  476.      filename argument, you can do filename completion; if you are
  477.     
  478.      typing a command, you can do command completion, if you are
  479.      typing in a symbol to GDB, you can do symbol name completion,
  480.      if you are typing in a variable to Bash, you can do variable
  481.      name completion...
  482.  
  483. `possible-completions (M-?)'
  484.      List the possible completions of the text before point.
  485.  
  486. File: readline.info,  Node: Miscellaneous Commands,  Prev: Commands For Completion,  Up: Readline Init Syntax
  487.  
  488. Some Miscellaneous Commands
  489. ...........................
  490.  
  491. `abort (C-g)'
  492.      Ding!  Stops things.
  493.  
  494. `do-uppercase-version (M-a, M-b, ...)'
  495.      Run the command that is bound to your uppercase brother.
  496.  
  497. `prefix-meta (ESC)'
  498.      Make the next character that you type be metafied.  This is
  499.      for people without a meta key.  ESC-f is equivalent to M-f.
  500.  
  501. `undo (C-_)'
  502.      Incremental undo, separately remembered for each line.
  503.  
  504. `revert-line (M-r)'
  505.      Undo all changes made to this line.  This is like typing the
  506.      `undo' command enough times to get back to the beginning.
  507.  
  508. File: readline.info,  Node: Readline Vi Mode,  Prev: Readline Init Syntax,  Up: Readline Init File
  509.  
  510. Readline Vi Mode
  511. ----------------
  512.  
  513. While the Readline library does not have a full set of Vi editing
  514. functions, it does contain enough to allow simple editing of the
  515. line.
  516.  
  517. In order to switch interactively between Emacs and Vi editing
  518. modes, use the command M-C-j (toggle-editing-mode).
  519.  
  520. When you enter a line in Vi mode, you are already placed in
  521. `insertion' mode, as if you had typed an `i'.  Pressing ESC
  522. switches you into `edit' mode, where you can edit the text of the
  523. line with the standard Vi movement keys, move to previous history
  524. lines with `k', and following lines with `j', and so forth.
  525.  
  526. This document describes the GNU Readline Library, a utility for
  527. aiding in the consitency of user interface across discrete
  528. programs that need to provide a command line interface.
  529.  
  530. Copyright (C) 1988 Free Software Foundation, Inc.
  531.  
  532. Permission is granted to make and distribute verbatim copies of
  533. this manual provided the copyright notice and this permission
  534. notice pare preserved on all copies.
  535.  
  536. Permission is granted to copy and distribute modified versions of
  537. this manual under the conditions for verbatim copying, provided
  538. that the entire resulting derived work is distributed under the
  539. terms of a permission notice identical to this one.
  540.  
  541. Permission is granted to copy and distribute translations of this
  542. manual into another language, under the above conditions for
  543. modified versions, except that this permission notice may be
  544. stated in a translation approved by the Foundation.
  545.  
  546. File: readline.info,  Node: Programming with GNU Readline,  Next: Concept Index,  Prev: Command Line Editing,  Up: Top
  547.  
  548. Programming with GNU Readline
  549. *****************************
  550.  
  551. This manual describes the interface between the GNU Readline
  552. Library and user programs.  If you are a programmer, and you wish
  553. to include the features found in GNU Readline in your own
  554. programs, such as completion, line editing, and interactive
  555. history manipulation, this documentation is for you.
  556.  
  557. * Menu:
  558.  
  559. * Default Behaviour::    Using the default behaviour of Readline.
  560. * Custom Functions::    Adding your own functions to Readline.
  561. * Custom Completers::    Supplanting or supplementing Readline's
  562.             completion functions.
  563.  
  564. File: readline.info,  Node: Default Behaviour,  Next: Custom Functions,  Up: Programming with GNU Readline
  565.  
  566. Default Behaviour
  567. =================
  568.  
  569. Many programs provide a command line interface, such as `mail',
  570. `ftp', and `sh'.  For such programs, the default behaviour of
  571. Readline is sufficient.  This section describes how to use
  572. Readline in the simplest way possible, perhaps to replace
  573. calls in your code to `gets ()'.
  574.  
  575. The function `readline' prints a prompt and then reads and returns
  576. a single line of text from the user.  The line which `readline ()'
  577. returns is allocated with `malloc ()'; you should `free ()' the
  578. line when you are done with it.  The declaration for `readline' in
  579. ANSI C is
  580.  
  581.      `char *readline (char *PROMPT);'
  582.  
  583. So, one might say
  584.  
  585.      `char *line = readline ("Enter a line: ");'
  586.  
  587. in order to read a line of text from the user.
  588.  
  589. The line which is returned has the final newline removed, so only
  590. the text of the line remains.
  591.  
  592. If readline encounters an `EOF' while reading the line, and the
  593. line is empty at that point, then `(char *)NULL' is returned. 
  594. Otherwise, the line is ended just as if a newline was typed.
  595.  
  596. If you want the user to be able to get at the line later, (with C-p
  597. for example), you must call `add_history ()' to save the line away
  598. in a "history" list of such lines.
  599.  
  600.      `add_history (line)';
  601.  
  602. For full details on the GNU History Library, see the associated
  603. manual.
  604.  
  605. It is polite to avoid saving empty lines on the history list, since
  606. it is rare than someone has a burning need to reuse a blank line. 
  607. Here is a function which usefully replaces the standard `gets ()'
  608. library function:
  609.  
  610.      /* A static variable for holding the line. */
  611.      static char *line_read = (char *)NULL;
  612.      
  613.      /* Read a string, and return a pointer to it.  Returns NULL on EOF. */
  614.      char *
  615.      do_gets ()
  616.      {
  617.        /* If the buffer has already been allocated, return the memory
  618.           to the free pool. */
  619.        if (line_read != (char *)NULL)
  620.          {
  621.            free (line_read);
  622.            line_read = (char *)NULL;
  623.          }
  624.      
  625.        /* Get a line from the user. */
  626.        line_read = readline ("");
  627.      
  628.        /* If the line has any text in it, save it on the history. */
  629.        if (line_read && *line_read)
  630.          add_history (line_read);
  631.      
  632.        return (line_read);
  633.      }
  634.  
  635. The above code gives the user the default behaviour of TAB
  636. completion: completion on file names.  If you do not want readline
  637. to complete on filenames, you can change the binding of the TAB
  638. key with `rl_bind_key ()'.
  639.  
  640.      `int rl_bind_key (int KEY, (int (*)())FUNCTION);'
  641.  
  642. `rl_bind_key ()' takes 2 arguments; KEY is the character that you
  643. want to bind, and FUNCTION is the address of the function to run
  644. when KEY is pressed.  Binding TAB to `rl_insert ()' makes TAB just
  645. insert itself.
  646.  
  647. `rl_bind_key ()' returns non-zero if KEY is not a valid ASCII
  648. character code (between 0 and 255).
  649.  
  650.      `rl_bind_key ('\t', rl_insert);'
  651.  
  652. This code should be executed once at the start of your program; you
  653. might write a function called `initialize_readline ()' which
  654. performs this and other desired initializations, such as
  655. installing
  656. custom completers, etc.
  657.  
  658. File: readline.info,  Node: Custom Functions,  Next: Custom Completers,  Prev: Default Behaviour,  Up: Programming with GNU Readline
  659.  
  660. Custom Functions
  661. ================
  662.  
  663. Readline provides a great many functions for manipulating the text
  664. of the line.  But it isn't possible to anticipate the needs of all
  665. programs.
  666. This section describes the various functions and variables defined
  667. in within the Readline library which allow a user program to add
  668. customized functionality to Readline.
  669.  
  670. * Menu:
  671.  
  672. * The Function Type::    C declarations to make code readable.
  673. * Function Naming::    How to give a function you write a name.
  674. * Keymaps::        Making keymaps.
  675. * Binding Keys::    Changing Keymaps.
  676. * Function Writing::    Variables and calling conventions.
  677. * Allowing Undoing::    How to make your functions undoable.
  678.  
  679. File: readline.info,  Node: The Function Type,  Next: Function Naming,  Up: Custom Functions
  680.  
  681. The Function Type
  682. -----------------
  683.  
  684. For the sake of readabilty, we declare a new type of object, called
  685. "Function".  A `Function' is a C language function which returns
  686. an `int'.  The type declaration for `Function' is:
  687.  
  688. `typedef int Function ();'
  689.  
  690. The reason for declaring this new type is to make it easier to
  691. write code describing pointers to C functions.  Let us say we had
  692. a variable called FUNC which was a pointer to a function.  Instead
  693. of the classic C declaration
  694.  
  695. `int (*)()func;'
  696.  
  697. we have
  698.  
  699. `Function *func;'
  700.  
  701. File: readline.info,  Node: Function Naming,  Next: Keymaps,  Prev: The Function Type,  Up: Custom Functions
  702.  
  703. Naming a Function
  704. -----------------
  705.  
  706. The user can dynamically change the bindings of keys while using
  707. Readline.  This is done by representing the function with a
  708. descriptive name.  The user is able to type the descriptive name
  709. when referring to the function.  Thus, in an init file, one might
  710. find
  711.  
  712.      Meta-Rubout:    backward-kill-word
  713.  
  714. This binds the keystroke Meta-Rubout to the function
  715. *descriptively* named `backward-kill-word'.  You, as the
  716. programmer, should bind the functions you write to descriptive
  717. names as well.  Readline provides a function for doing that:
  718.  
  719.  * Function: rl_add_defun (CHAR *NAME, FUNCTION *FUNCTION, INT KEY)
  720.      Add NAME to the list of named functions.  Make FUNCTION be the
  721.      function that gets called.  If KEY is not -1, then bind it to
  722.      FUNCTION using `rl_bind_key ()'.
  723.  
  724. Using this function alone is sufficient for most applications.  It
  725. is the recommended way to add a few functions to the default
  726. functions that Readline has built in already.  If you need to do
  727. more or different things than adding a function to Readline, you
  728. may
  729. need to use the underlying functions described below.
  730.  
  731. File: readline.info,  Node: Keymaps,  Next: Binding Keys,  Prev: Function Naming,  Up: Custom Functions
  732.  
  733. Selecting a Keymap
  734. ------------------
  735.  
  736. Key bindings take place on a "keymap".  The keymap is the
  737. association between the keys that the user types and the functions
  738. that get run.  You can make your own keymaps, copy existing
  739. keymaps, and tell Readline which keymap to use.
  740.  
  741.  * Function: Keymap rl_make_bare_keymap ()
  742.      Returns a new, empty keymap.  The space for the keymap is
  743.      allocated with `malloc ()'; you should `free ()' it when you
  744.      are done.
  745.  
  746.  * Function: Keymap rl_copy_keymap (KEYMAP MAP)
  747.      Return a new keymap which is a copy of MAP.
  748.  
  749.  * Function: Keymap rl_make_keymap ()
  750.      Return a new keymap with the printing characters bound to
  751.      rl_insert, the lowercase Meta characters bound to run their
  752.      equivalents, and the Meta digits bound to produce numeric
  753.      arguments.
  754.  
  755. File: readline.info,  Node: Binding Keys,  Next: Function Writing,  Prev: Keymaps,  Up: Custom Functions
  756.  
  757. Binding Keys
  758. ------------
  759.  
  760. You associate keys with functions through the keymap.  Here are
  761. functions for doing that.
  762.  
  763.  * Function: int rl_bind_key (INT KEY, FUNCTION *FUNCTION)
  764.      Binds KEY to FUNCTION in the currently selected keymap. 
  765.      Returns non-zero in the case of an invalid KEY.
  766.  
  767.  * Function: int rl_bind_key_in_map (INT KEY, FUNCTION *FUNCTION,
  768.           KEYMAP MAP)
  769.      Bind KEY to FUNCTION in MAP.  Returns non-zero in the case of
  770.      an invalid KEY.
  771.  
  772.  * Function: int rl_unbind_key (INT KEY)
  773.      Make KEY do nothing in the currently selected keymap.  Returns
  774.      non-zero in case of error.
  775.  
  776.  * Function: int rl_unbind_key_in_map (INT KEY, KEYMAP MAP)
  777.      Make KEY be bound to the null function in MAP.  Returns
  778.      non-zero in case of error.
  779.  
  780.  * Function: rl_generic_bind (INT TYPE, CHAR *KEYSEQ, CHAR *DATA,
  781.           KEYMAP MAP)
  782.      Bind the key sequence represented by the string KEYSEQ to the
  783.      arbitrary pointer DATA.  TYPE says what kind of data is
  784.      pointed to by DATA; right now this can be a function
  785.      (`ISFUNC'), a macro (`ISMACR'), or a keymap (`ISKMAP').  This
  786.      makes new keymaps as necessary.  The initial place to do
  787.      bindings is in MAP.
  788.  
  789. File: readline.info,  Node: Function Writing,  Next: Allowing Undoing,  Prev: Binding Keys,  Up: Custom Functions
  790.  
  791. Writing a New Function
  792. ----------------------
  793.  
  794. In order to write new functions for Readline, you need to know the
  795. calling conventions for keyboard invoked functions, and the names
  796. of the variables that describe the current state of the line
  797. gathered so far.
  798.  
  799.  * Variable: char *rl_line_buffer
  800.      This is the line gathered so far.  You are welcome to modify
  801.      the contents of this, but see Undoing, below.
  802.  
  803.  * Variable: int rl_point
  804.      The offset of the current cursor position in RL_LINE_BUFFER.
  805.  
  806.  * Variable: int rl_end
  807.      The number of characters present in `rl_line_buffer'.  When
  808.      `rl_point' is at the end of the line, then `rl_point' and
  809.      `rl_end' are equal.
  810.  
  811. The calling sequence for a command `foo' looks like
  812.  
  813.      `foo (int count, int key)'
  814.  
  815. where COUNT is the numeric argument (or 1 if defaulted) and KEY is
  816. the key that invoked this function.
  817.  
  818. It is completely up to the function as to what should be done with
  819. the numeric argument; some functions use it as a repeat count,
  820. other functions as a flag, and some choose to ignore it.  In
  821. general, if a function uses the numeric argument as a repeat
  822. count, it should be able to do something useful with a negative
  823. argument as well as a positive argument.  At the very least, it
  824. should be aware that it can be passed a negative argument.
  825.  
  826. File: readline.info,  Node: Allowing Undoing,  Prev: Function Writing,  Up: Custom Functions
  827.  
  828. Allowing Undoing
  829. ----------------
  830.  
  831. Supporting the undo command is a painless thing to do, and makes
  832. your functions much more useful to the end user.  It is certainly
  833. easy to try something if you know you can undo it.  I could use an
  834. undo function for the stock market.
  835.  
  836. If your function simply inserts text once, or deletes text once,
  837. and it calls `rl_insert_text ()' or `rl_delete_text ()' to do it,
  838. then undoing is already done for you automatically, and you can
  839. safely skip this section.
  840.  
  841. If you do multiple insertions or multiple deletions, or any
  842. combination of these operations, you should group them together
  843. into one operation.  This can be done with `rl_begin_undo_group
  844. ()' and `rl_end_undo_group ()'.
  845.  
  846.  * Function: rl_begin_undo_group ()
  847.      Begins saving undo information in a group construct.  The undo
  848.      information usually comes from calls to `rl_insert_text ()'
  849.      and `rl_delete_text ()', but they could be direct calls to
  850.      `rl_add_undo ()'.
  851.  
  852.  * Function: rl_end_undo_group ()
  853.      Closes the current undo group started with
  854.      `rl_begin_undo_group ()'.  There should be exactly one call
  855.      to `rl_end_undo_group ()' for every call to
  856.      `rl_begin_undo_group ()'.
  857.  
  858. Finally, if you neither insert nor delete text, but directly modify
  859. the existing text (e.g. change its case), you call `rl_modifying
  860. ()' once, just before you modify the text.  You must supply the
  861. indices of the text range that you are going to modify.
  862.  
  863.  * Function: rl_modifying (INT START, INT END)
  864.      Tell Readline to save the text between START and END as a
  865.      single undo unit.  It is assumed that subsequent to this call
  866.      you will modify that range of text in some way.
  867.  
  868. An Example
  869. ----------
  870.  
  871. Here is a function which changes lowercase characters to the
  872. uppercase equivalents, and uppercase characters to the lowercase
  873. equivalents.
  874. If this function was bound to `M-c', then typing `M-c' would
  875. change the case of the character under point.  Typing `10 M-c'
  876. would change the case of the following 10 characters, leaving the
  877. cursor on the last character changed.
  878.  
  879.      /* Invert the case of the COUNT following characters. */
  880.      invert_case_line (count, key)
  881.           int count, key;
  882.      {
  883.        register int start, end;
  884.      
  885.        start = rl_point;
  886.      
  887.        if (count < 0)
  888.          {
  889.            direction = -1;
  890.            count = -count;
  891.          }
  892.        else
  893.          direction = 1;
  894.      
  895.        /* Find the end of the range to modify. */
  896.        end = start + (count * direction);
  897.      
  898.        /* Force it to be within range. */
  899.        if (end > rl_end)
  900.          end = rl_end;
  901.        else if (end < 0)
  902.          end = -1;
  903.      
  904.        if (start > end)
  905.          {
  906.            int temp = start;
  907.            start = end;
  908.            end = temp;
  909.          }
  910.      
  911.        if (start == end)
  912.          return;
  913.      
  914.        /* Tell readline that we are modifying the line, so save the undo
  915.           information. */
  916.        rl_modifying (start, end);
  917.      
  918.        for (; start != end; start += direction)
  919.          {
  920.            if (uppercase_p (rl_line_buffer[start]))
  921.              rl_line_buffer[start] = to_lower (rl_line_buffer[start]);
  922.            else if (lowercase_p (rl_line_buffer[start]))
  923.              rl_line_buffer[start] = to_upper (rl_line_buffer[start]);
  924.          }
  925.        /* Move point to on top of the last character changed. */
  926.        rl_point = end - direction;
  927.      }
  928.  
  929. File: readline.info,  Node: Custom Completers,  Prev: Custom Functions,  Up: Programming with GNU Readline
  930.  
  931. Custom Completers
  932. =================
  933.  
  934. Typically, a program that reads commands from the user has a way of
  935. disambiguating between commands and data.  If your program is one
  936. of these, then it can provide completion for either commands, or
  937. data, or both commands and data.  The following sections describe
  938. how your program and Readline cooperate to provide this service to
  939. end users.
  940.  
  941. * Menu:
  942.  
  943. * How Completing Works::    The logic used to do completion.
  944. * Completion Functions::    Functions provided by Readline.
  945. * Completion Variables::    Variables which control completion.
  946. * A Short Completion Example::    An example of writing completer subroutines.
  947.  
  948. File: readline.info,  Node: How Completing Works,  Next: Completion Functions,  Up: Custom Completers
  949.  
  950. How Completing Works
  951. --------------------
  952.  
  953. In order to complete some text, the full list of possible
  954. completions must be available.  That is to say, it is not possible
  955. to accurately expand a partial word without knowing what all of
  956. the possible words that make sense in that context are.  The GNU
  957. Readline library provides the user interface to completion, and
  958. additionally, two of the most common completion functions;
  959. filename and username.  For completing other types of text, you
  960. must write your own completion function.  This section describes
  961. exactly what those functions must do, and provides an example
  962. function.
  963.  
  964. There are three major functions used to perform completion:
  965.  
  966.   1. The user-interface function `rl_complete ()'.  This function
  967.      is called interactively with the same calling conventions as
  968.      other functions in readline intended for interactive use;
  969.      i.e. COUNT, and `invoking-key'.  It isolates the word to be
  970.      completed and calls `completion_matches ()' to generate a
  971.      list of possible completions.  It then either lists the
  972.      possible completions or actually performs the completion,
  973.      depending on which behaviour is desired.
  974.  
  975.   2. The internal function `completion_matches ()' uses your
  976.      "generator" function to generate the list of possible
  977.      matches, and then returns the array of these matches.  You
  978.      should place the address of your generator function in
  979.      `rl_completion_entry_function'.
  980.  
  981.   3. The generator function is called repeatedly from
  982.      `completion_matches ()', returning a string each time.  The
  983.      arguments to the generator function are TEXT and STATE.  TEXT
  984.      is the partial word to be completed.  STATE is zero the first
  985.      time the function is called, and a positive non-zero integer
  986.      for each subsequent call.  When the generator function
  987.      returns `(char *)NULL' this signals `completion_matches ()'
  988.      that there are no more possibilities left.
  989.  
  990.  * Function: rl_complete (INT IGNORE, INT INVOKING_KEY)
  991.      Complete the word at or before point.  You have supplied the
  992.      function that does the initial simple matching selection
  993.      algorithm (see `completion_matches ()').  The default is to
  994.      do filename completion.
  995.  
  996. Note that `rl_complete ()' has the identical calling conventions as
  997. any other key-invokable function; this is because by default it is
  998. bound to the `TAB' key.
  999.  
  1000.  * Variable: Function *rl_completion_entry_function
  1001.      This is a pointer to the generator function for
  1002.      `completion_matches ()'.  If the value of
  1003.      `rl_completion_entry_function' is `(Function *)NULL' then the
  1004.      default filename generator function is used, namely
  1005.      `filename_entry_function ()'.
  1006.  
  1007. File: readline.info,  Node: Completion Functions,  Next: Completion Variables,  Prev: How Completing Works,  Up: Custom Completers
  1008.  
  1009. Completion Functions
  1010. --------------------
  1011.  
  1012. Here is the complete list of callable completion functions present
  1013. in Readline.
  1014.  
  1015.  * Function: rl_complete_internal (INT WHAT_TO_DO)
  1016.      Complete the word at or before point.  WHAT_TO_DO says what to
  1017.      do with the completion.  A value of `?' means list the
  1018.      possible completions.  `TAB' means do standard completion. 
  1019.      `*' means insert all of the possible completions.
  1020.  
  1021.  * Function: rl_complete (INT IGNORE, INT INVOKING_KEY)
  1022.      Complete the word at or before point.  You have supplied the
  1023.      function that does the initial simple matching selection
  1024.      algorithm (see `completion_matches ()').  The default is to
  1025.      do filename completion.  This just calls
  1026.      `rl_complete_internal ()' with an argument of `TAB'.
  1027.  
  1028.  * Function: rl_possible_completions ()
  1029.      List the possible completions.  See description of
  1030.      `rl_complete ()'.  This just calls `rl_complete_internal ()'
  1031.      with an argument of `?'.
  1032.  
  1033.  * Function: char **completion_matches (CHAR *TEXT, CHAR
  1034.           *(*ENTRY_FUNCTION) ())
  1035.      Returns an array of `(char *)' which is a list of completions
  1036.      for TEXT.  If there are no completions, returns `(char
  1037.      **)NULL'.  The first entry in the returned array is the
  1038.      substitution for TEXT.  The remaining entries are the
  1039.      possible completions.  The array is terminated with a `NULL'
  1040.      pointer.
  1041.  
  1042.      ENTRY_FUNCTION is a function of two args, and returns a `(char
  1043.      *)'.  The first argument is TEXT.  The second is a state
  1044.      argument; it is zero on the first call, and non-zero on
  1045.      subsequent calls.  It returns a `NULL'  pointer to the caller
  1046.      when there are no more matches.
  1047.  
  1048.  * Function: char *filename_completion_function (CHAR *TEXT, INT
  1049.           STATE)
  1050.      A generator function for filename completion in the general
  1051.      case.  Note that completion in the Bash shell is a little
  1052.      different because of all the pathnames that must be followed
  1053.      when looking up the completion for a command.
  1054.  
  1055.  * Function: char *username_completion_function (CHAR *TEXT, INT
  1056.           STATE)
  1057.      A completion generator for usernames.  TEXT contains a partial
  1058.      username preceded by a random character (usually `~').
  1059.  
  1060. File: readline.info,  Node: Completion Variables,  Next: A Short Completion Example,  Prev: Completion Functions,  Up: Custom Completers
  1061.  
  1062. Completion Variables
  1063. --------------------
  1064.  
  1065.  * Variable: Function *rl_completion_entry_function
  1066.      A pointer to the generator function for `completion_matches ()'.
  1067.      `NULL' means to use `filename_entry_function ()', the default
  1068.      filename completer.
  1069.  
  1070.  * Variable: Function *rl_attempted_completion_function
  1071.      A pointer to an alternative function to create matches.  The
  1072.      function is called with TEXT, START, and END.  START and END
  1073.      are indices in `rl_line_buffer' saying what the boundaries of
  1074.      TEXT are.  If this function exists and returns `NULL' then
  1075.      `rl_complete ()' will call the value of
  1076.      `rl_completion_entry_function' to generate matches, otherwise
  1077.      the array of strings returned will be used.
  1078.  
  1079.  * Variable: int rl_completion_query_items
  1080.      Up to this many items will be displayed in response to a
  1081.      possible-completions call.  After that, we ask the user if
  1082.      she is sure she wants to see them all.  The default value is
  1083.      100.
  1084.  
  1085.  * Variable: char *rl_basic_word_break_characters
  1086.      The basic list of characters that signal a break between words
  1087.      for the completer routine.  The contents of this variable is
  1088.      what breaks words in the Bash shell, i.e. " \t\n\"\\'`@$><=".
  1089.  
  1090.  * Variable: char *rl_completer_word_break_characters
  1091.      The list of characters that signal a break between words for
  1092.      `rl_complete_internal ()'.  The default list is the contents
  1093.      of `rl_basic_word_break_characters'.
  1094.  
  1095.  * Variable: char *rl_special_prefixes
  1096.      The list of characters that are word break characters, but
  1097.      should be left in TEXT when it is passed to the completion
  1098.      function.  Programs can use this to help determine what kind
  1099.     
  1100.      of completing to do.
  1101.  
  1102.  * Variable: int rl_ignore_completion_duplicates
  1103.      If non-zero, then disallow duplicates in the matches.  Default
  1104.      is 1.
  1105.  
  1106.  * Variable: int rl_filename_completion_desired
  1107.      Non-zero means that the results of the matches are to be
  1108.      treated as filenames.  This is *always* zero on entry, and
  1109.      can only be changed within a completion entry generator
  1110.      function.
  1111.  
  1112.  * Variable: Function *rl_ignore_some_completions_function
  1113.      This function, if defined, is called by the completer when
  1114.      real filename completion is done, after all the matching
  1115.      names have been generated.  It is passed a `NULL' terminated
  1116.      array of pointers to `(char *)' known as MATCHES in the code.
  1117.      The 1st element (`matches[0]') is the maximal substring that
  1118.      is common to all matches. This function can re-arrange the
  1119.      list of matches as required, but each deleted element of the
  1120.      array must be `free()''d.
  1121.  
  1122. File: readline.info,  Node: A Short Completion Example,  Prev: Completion Variables,  Up: Custom Completers
  1123.  
  1124. A Short Completion Example
  1125. --------------------------
  1126.  
  1127. Here is a small application demonstrating the use of the GNU
  1128. Readline library.  It is called `fileman', and the source code
  1129. resides in `readline/examples/fileman.c'.  This sample application
  1130. provides completion of command names, line editing features, and
  1131. access to the history list.
  1132.  
  1133.      /* fileman.c -- A tiny application which demonstrates how to use the
  1134.         GNU Readline library.  This application interactively allows users
  1135.         to manipulate files and their modes. */
  1136.      
  1137.      #include <stdio.h>
  1138.      #include <readline/readline.h>
  1139.      #include <readline/history.h>
  1140.      #include <sys/types.h>
  1141.      #include <sys/file.h>
  1142.      #include <sys/stat.h>
  1143.      #include <sys/errno.h>
  1144.      
  1145.      /* The names of functions that actually do the manipulation. */
  1146.      int com_list (), com_view (), com_rename (), com_stat (), com_pwd ();
  1147.      int com_delete (), com_help (), com_cd (), com_quit ();
  1148.      
  1149.      /* A structure which contains information on the commands this program
  1150.         can understand. */
  1151.      
  1152.      typedef struct {
  1153.        char *name;                   /* User printable name of the function. */
  1154.        Function *func;               /* Function to call to do the job. */
  1155.        char *doc;                    /* Documentation for this function.  */
  1156.      } COMMAND;
  1157.      
  1158.      COMMAND commands[] = {
  1159.        { "cd", com_cd, "Change to directory DIR" },
  1160.        { "delete", com_delete, "Delete FILE" },
  1161.        { "help", com_help, "Display this text" },
  1162.        { "?", com_help, "Synonym for `help'" },
  1163.        { "list", com_list, "List files in DIR" },
  1164.        { "ls", com_list, "Synonym for `list'" },
  1165.        { "pwd", com_pwd, "Print the current working directory" },
  1166.        { "quit", com_quit, "Quit using Fileman" },
  1167.        { "rename", com_rename, "Rename FILE to NEWNAME" },
  1168.        { "stat", com_stat, "Print out statistics on FILE" },
  1169.        { "view", com_view, "View the contents of FILE" },
  1170.        { (char *)NULL, (Function *)NULL, (char *)NULL }
  1171.      };
  1172.      
  1173.      /* The name of this program, as taken from argv[0]. */
  1174.      char *progname;
  1175.      
  1176.      /* When non-zero, this global means the user is done using this program. */
  1177.      int done = 0;
  1178.      
  1179.      main (argc, argv)
  1180.           int argc;
  1181.           char **argv;
  1182.      {
  1183.        progname = argv[0];
  1184.      
  1185.        initialize_readline ();       /* Bind our completer. */
  1186.      
  1187.        /* Loop reading and executing lines until the user quits. */
  1188.        while (!done)
  1189.          {
  1190.            char *line;
  1191.      
  1192.            line = readline ("FileMan: ");
  1193.      
  1194.            if (!line)
  1195.              {
  1196.                done = 1;             /* Encountered EOF at top level. */
  1197.              }
  1198.            else
  1199.              {
  1200.                /* Remove leading and trailing whitespace from the line.
  1201.                   Then, if there is anything left, add it to the history list
  1202.                   and execute it. */
  1203.                stripwhite (line);
  1204.      
  1205.                if (*line)
  1206.                  {
  1207.                    add_history (line);
  1208.                    execute_line (line);
  1209.                  }
  1210.              }
  1211.      
  1212.            if (line)
  1213.              free (line);
  1214.          }
  1215.        exit (0);
  1216.      }
  1217.      
  1218.      /* Execute a command line. */
  1219.      execute_line (line)
  1220.           char *line;
  1221.      {
  1222.        register int i;
  1223.        COMMAND *find_command (), *command;
  1224.        char *word;
  1225.      
  1226.        /* Isolate the command word. */
  1227.        i = 0;
  1228.        while (line[i] && !whitespace (line[i]))
  1229.          i++;
  1230.      
  1231.        word = line;
  1232.      
  1233.        if (line[i])
  1234.          line[i++] = '\0';
  1235.      
  1236.        command = find_command (word);
  1237.      
  1238.        if (!command)
  1239.          {
  1240.            fprintf (stderr, "%s: No such command for FileMan.\n", word);
  1241.            return;
  1242.          }
  1243.      
  1244.        /* Get argument to command, if any. */
  1245.        while (whitespace (line[i]))
  1246.          i++;
  1247.      
  1248.        word = line + i;
  1249.      
  1250.        /* Call the function. */
  1251.        (*(command->func)) (word);
  1252.      }
  1253.      
  1254.      /* Look up NAME as the name of a command, and return a pointer to that
  1255.         command.  Return a NULL pointer if NAME isn't a command name. */
  1256.      COMMAND *
  1257.      find_command (name)
  1258.           char *name;
  1259.      {
  1260.        register int i;
  1261.      
  1262.        for (i = 0; commands[i].name; i++)
  1263.          if (strcmp (name, commands[i].name) == 0)
  1264.            return (&commands[i]);
  1265.      
  1266.        return ((COMMAND *)NULL);
  1267.      }
  1268.      
  1269.      /* Strip whitespace from the start and end of STRING. */
  1270.      stripwhite (string)
  1271.           char *string;
  1272.      {
  1273.        register int i = 0;
  1274.      
  1275.        while (whitespace (string[i]))
  1276.          i++;
  1277.      
  1278.        if (i)
  1279.          strcpy (string, string + i);
  1280.      
  1281.        i = strlen (string) - 1;
  1282.      
  1283.        while (i > 0 && whitespace (string[i]))
  1284.          i--;
  1285.      
  1286.        string[++i] = '\0';
  1287.      }
  1288.      
  1289.      /* **************************************************************** */
  1290.      /*                                                                  */
  1291.      /*                  Interface to Readline Completion                */
  1292.      /*                                                                  */
  1293.      /* **************************************************************** */
  1294.      
  1295.      /* Tell the GNU Readline library how to complete.  We want to try to complete
  1296.         on command names if this is the first word in the line, or on filenames
  1297.         if not. */
  1298.      initialize_readline ()
  1299.      {
  1300.        char **fileman_completion ();
  1301.      
  1302.        /* Allow conditional parsing of the ~/.inputrc file. */
  1303.        rl_readline_name = "FileMan";
  1304.      
  1305.        /* Tell the completer that we want a crack first. */
  1306.        rl_attempted_completion_function = (Function *)fileman_completion;
  1307.      }
  1308.      
  1309.      /* Attempt to complete on the contents of TEXT.  START and END show the
  1310.         region of TEXT that contains the word to complete.  We can use the
  1311.         entire line in case we want to do some simple parsing.  Return the
  1312.         array of matches, or NULL if there aren't any. */
  1313.      char **
  1314.      fileman_completion (text, start, end)
  1315.           char *text;
  1316.           int start, end;
  1317.      {
  1318.        char **matches;
  1319.        char *command_generator ();
  1320.      
  1321.        matches = (char **)NULL;
  1322.      
  1323.        /* If this word is at the start of the line, then it is a command
  1324.           to complete.  Otherwise it is the name of a file in the current
  1325.           directory. */
  1326.        if (start == 0)
  1327.          matches = completion_matches (text, command_generator);
  1328.      
  1329.        return (matches);
  1330.      }
  1331.      
  1332.      /* Generator function for command completion.  STATE lets us know whether
  1333.         to start from scratch; without any state (i.e. STATE == 0), then we
  1334.         start at the top of the list. */
  1335.      char *
  1336.      command_generator (text, state)
  1337.           char *text;
  1338.           int state;
  1339.      {
  1340.        static int list_index, len;
  1341.        char *name;
  1342.      
  1343.        /* If this is a new word to complete, initialize now.  This includes
  1344.           saving the length of TEXT for efficiency, and initializing the index
  1345.           variable to 0. */
  1346.        if (!state)
  1347.          {
  1348.            list_index = 0;
  1349.            len = strlen (text);
  1350.          }
  1351.      
  1352.        /* Return the next name which partially matches from the command list. */
  1353.        while (name = commands[list_index].name)
  1354.          {
  1355.            list_index++;
  1356.      
  1357.            if (strncmp (name, text, len) == 0)
  1358.              return (name);
  1359.          }
  1360.      
  1361.        /* If no names matched, then return NULL. */
  1362.        return ((char *)NULL);
  1363.      }
  1364.      
  1365.      /* **************************************************************** */
  1366.      /*                                                                  */
  1367.      /*                       FileMan Commands                           */
  1368.      /*                                                                  */
  1369.      /* **************************************************************** */
  1370.      
  1371.      /* String to pass to system ().  This is for the LIST, VIEW and RENAME
  1372.         commands. */
  1373.      static char syscom[1024];
  1374.      
  1375.      /* List the file(s) named in arg. */
  1376.      com_list (arg)
  1377.           char *arg;
  1378.      {
  1379.        sprintf (syscom, "ls -FClg %s", arg);
  1380.        system (syscom);
  1381.      }
  1382.      
  1383.      com_view (arg)
  1384.           char *arg;
  1385.      {
  1386.        if (!valid_argument ("view", arg))
  1387.          return;
  1388.      
  1389.        sprintf (syscom, "cat %s | more", arg);
  1390.        system (syscom);
  1391.      }
  1392.      
  1393.      com_rename (arg)
  1394.           char *arg;
  1395.      {
  1396.        too_dangerous ("rename");
  1397.      }
  1398.      
  1399.      com_stat (arg)
  1400.           char *arg;
  1401.      {
  1402.        struct stat finfo;
  1403.      
  1404.        if (!valid_argument ("stat", arg))
  1405.          return;
  1406.      
  1407.        if (stat (arg, &finfo) == -1)
  1408.          {
  1409.            perror (arg);
  1410.            return;
  1411.          }
  1412.      
  1413.        printf ("Statistics for `%s':\n", arg);
  1414.      
  1415.        printf ("%s has %d link%s, and is %d bytes in length.\n", arg,
  1416.                finfo.st_nlink, (finfo.st_nlink == 1) ? "" : "s",  finfo.st_size);
  1417.        printf ("      Created on: %s", ctime (&finfo.st_ctime));
  1418.        printf ("  Last access at: %s", ctime (&finfo.st_atime));
  1419.        printf ("Last modified at: %s", ctime (&finfo.st_mtime));
  1420.      }
  1421.      
  1422.      com_delete (arg)
  1423.           char *arg;
  1424.      {
  1425.        too_dangerous ("delete");
  1426.      }
  1427.      
  1428.      /* Print out help for ARG, or for all of the commands if ARG is
  1429.         not present. */
  1430.      com_help (arg)
  1431.           char *arg;
  1432.      {
  1433.        register int i;
  1434.        int printed = 0;
  1435.      
  1436.        for (i = 0; commands[i].name; i++)
  1437.          {
  1438.            if (!*arg || (strcmp (arg, commands[i].name) == 0))
  1439.              {
  1440.                printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
  1441.                printed++;
  1442.              }
  1443.          }
  1444.      
  1445.        if (!printed)
  1446.          {
  1447.            printf ("No commands match `%s'.  Possibilties are:\n", arg);
  1448.      
  1449.            for (i = 0; commands[i].name; i++)
  1450.              {
  1451.                /* Print in six columns. */
  1452.                if (printed == 6)
  1453.                  {
  1454.                    printed = 0;
  1455.                    printf ("\n");
  1456.                  }
  1457.      
  1458.                printf ("%s\t", commands[i].name);
  1459.                printed++;
  1460.              }
  1461.      
  1462.            if (printed)
  1463.              printf ("\n");
  1464.          }
  1465.      }
  1466.      
  1467.      /* Change to the directory ARG. */
  1468.      com_cd (arg)
  1469.           char *arg;
  1470.      {
  1471.        if (chdir (arg) == -1)
  1472.          perror (arg);
  1473.      
  1474.        com_pwd ("");
  1475.      }
  1476.      
  1477.      /* Print out the current working directory. */
  1478.      com_pwd (ignore)
  1479.           char *ignore;
  1480.      {
  1481.        char dir[1024];
  1482.      
  1483.        (void) getwd (dir);
  1484.      
  1485.        printf ("Current directory is %s\n", dir);
  1486.      }
  1487.      
  1488.      /* The user wishes to quit using this program.  Just set DONE non-zero. */
  1489.      com_quit (arg)
  1490.           char *arg;
  1491.      {
  1492.        done = 1;
  1493.      }
  1494.      
  1495.      /* Function which tells you that you can't do this. */
  1496.      too_dangerous (caller)
  1497.           char *caller;
  1498.      {
  1499.        fprintf (stderr,
  1500.                 "%s: Too dangerous for me to distribute.  Write it yourself.\n",
  1501.                 caller);
  1502.      }
  1503.      
  1504.      /* Return non-zero if ARG is a valid argument for CALLER, else print
  1505.         an error message and return zero. */
  1506.      int
  1507.      valid_argument (caller, arg)
  1508.           char *caller, *arg;
  1509.      {
  1510.        if (!arg || !*arg)
  1511.          {
  1512.            fprintf (stderr, "%s: Argument required.\n", caller);
  1513.            return (0);
  1514.          }
  1515.      
  1516.        return (1);
  1517.      }
  1518.  
  1519. File: readline.info,  Node: Concept Index,  Next: Function and Variable Index,  Prev: Programming with GNU Readline,  Up: Top
  1520.  
  1521. Concept Index
  1522. *************
  1523.  
  1524. * Menu:
  1525.  
  1526. * interaction, readline:                Readline Interaction.
  1527. * readline, function:                   Default Behaviour.
  1528.  
  1529. File: readline.info,  Node: Function and Variable Index,  Prev: Concept Index,  Up: Top
  1530.  
  1531. Function and Variable Index
  1532. ***************************
  1533.  
  1534. * Menu:
  1535.  
  1536. * Function *rl_attempted_completion_function: Completion Variables.
  1537. * Function *rl_completion_entry_function: How Completing Works.
  1538. * Function *rl_completion_entry_function: Completion Variables.
  1539. * Function *rl_ignore_some_completions_function: Completion Variables.
  1540. * Keymap rl_copy_keymap:                Keymaps.
  1541. * Keymap rl_make_bare_keymap:           Keymaps.
  1542. * Keymap rl_make_keymap:                Keymaps.
  1543. * char **completion_matches:            Completion Functions.
  1544. * char *filename_completion_function:   Completion Functions.
  1545. * char *rl_basic_word_break_characters: Completion Variables.
  1546. * char *rl_completer_word_break_characters: Completion Variables.
  1547. * char *rl_line_buffer:                 Function Writing.
  1548. * char *rl_special_prefixes:            Completion Variables.
  1549. * char *username_completion_function:   Completion Functions.
  1550. * int rl_bind_key:                      Binding Keys.
  1551. * int rl_bind_key_in_map:               Binding Keys.
  1552. * int rl_completion_query_items:        Completion Variables.
  1553. * int rl_end:                           Function Writing.
  1554. * int rl_filename_completion_desired:   Completion Variables.
  1555. * int rl_ignore_completion_duplicates:  Completion Variables.
  1556. * int rl_point:                         Function Writing.
  1557. * int rl_unbind_key:                    Binding Keys.
  1558. * int rl_unbind_key_in_map:             Binding Keys.
  1559. * readline ():                          Default Behaviour.
  1560. * rl_add_defun:                         Function Naming.
  1561. * rl_begin_undo_group:                  Allowing Undoing.
  1562. * rl_bind_key ():                       Default Behaviour.
  1563. * rl_complete:                          How Completing Works.
  1564. * rl_complete:                          Completion Functions.
  1565. * rl_complete_internal:                 Completion Functions.
  1566. * rl_end_undo_group:                    Allowing Undoing.
  1567. * rl_generic_bind:                      Binding Keys.
  1568. * rl_modifying:                         Allowing Undoing.
  1569. * rl_possible_completions:              Completion Functions.
  1570.  
  1571.  
  1572. Tag Table:
  1573. Node: Top1012
  1574. Node: Command Line Editing1622
  1575. Node: Introduction and Notation2042
  1576. Node: Readline Interaction3038
  1577. Node: Readline Bare Essentials4173
  1578. Node: Readline Movement Commands5672
  1579. Node: Readline Killing Commands6557
  1580. Node: Readline Arguments8384
  1581. Node: Readline Init File9330
  1582. Node: Readline Init Syntax10025
  1583. Node: Commands For Moving11475
  1584. Node: Commands For History12105
  1585. Node: Commands For Text13191
  1586. Node: Commands For Killing14869
  1587. Node: Numeric Arguments15996
  1588. Node: Commands For Completion16440
  1589. Node: Miscellaneous Commands17174
  1590. Node: Readline Vi Mode17862
  1591. Node: Programming with GNU Readline19448
  1592. Node: Default Behaviour20150
  1593. Node: Custom Functions23336
  1594. Node: The Function Type24131
  1595. Node: Function Naming24749
  1596. Node: Keymaps25992
  1597. Node: Binding Keys26904
  1598. Node: Function Writing28207
  1599. Node: Allowing Undoing29636
  1600. Node: Custom Completers33127
  1601. Node: How Completing Works33880
  1602. Node: Completion Functions36688
  1603. Node: Completion Variables39046
  1604. Node: A Short Completion Example41843
  1605. Node: Concept Index53423
  1606. Node: Function and Variable Index53712
  1607. End Tag Table
  1608.